package com.crm.controller.adminController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.CurrencyDAO;
import com.crm.entity.Business;
import com.crm.entity.Currency;
@Controller
@RequestMapping("/currency")
public class CurrencyController {
	protected final Log logger = LogFactory.getLog(getClass());
	CurrencyDAO cDAO=new CurrencyDAO();
	Currency cur=null;
	
	@RequestMapping("/open.do")
	public ModelAndView open(HttpSession session,ModelAndView model){
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		model.setViewName("/jsp/CurrencyValue.jsp");
		model.addObject("submenu", "admin");
		model.addObject("menuName","admin");
		return model;
		
	}
	
	@RequestMapping("/show.do")
	public ModelAndView show(HttpServletRequest req,HttpSession session ,ModelMap modattr) throws Exception{
	
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		 logger.info("Business Is:"+bus);
		
		
		cur=cDAO.getCurrncy(bus.getBusinessId());
		 logger.info("the currency which exist in database  is"+cur);
		ModelAndView model;
		if(cur!=null)
		{
			 logger.info("the currency id is"+cur.getId());
			 logger.info("the currency id is"+cur.getDollar());
			 logger.info("the currency id is"+cur.getSdollar());
		 model= new ModelAndView("/jsp/CurrencyValue.jsp", "command", cur);
		
		}
		else{
			 model= new ModelAndView("/jsp/CurrencyValue.jsp", "command",new Currency());
		}
		model.addObject("submenu", "admin");
		model.addObject("menuName","admin");
		return model;
		
}
	/*@RequestMapping(value="/save.do",method = RequestMethod.POST)
	public ModelAndView insert(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		float doller=Float.parseFloat(req.getParameter("doller"));
		float euro=Float.parseFloat(req.getParameter("euro"));
		float sdoller=Float.parseFloat(req.getParameter("singdoller"));
		float yen=Float.parseFloat(req.getParameter("yen"));
		float pound=Float.parseFloat(req.getParameter("pound"));
		//float dol=Float.parseFloat(doller);
		//float eur=Float.parseFloat(euro);
		Currency c=new Currency(doller,euro,sdoller,yen,pound);
		CurrencyDAO cDAO= new CurrencyDAO();
		cDAO.insert(c);
		
		model.setViewName("/jsp/saveInfo.jsp");
		return model;
		
}*/
	@RequestMapping(value="/save.do",method = RequestMethod.POST)
	public ModelAndView addCurrency(HttpSession session,@ModelAttribute("contact")
            Currency currency, BindingResult result) throws Exception {
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		currency.setBusinessId(bus);
	
	
	cur=cDAO.getCurrncy(bus.getBusinessId());
	if(cur!=null){
		cDAO.delete(cur);
	}
	
	logger.info("deleting all currency before insert");
	logger.info("the currency id is"+currency.getId());
	ModelAndView model=new ModelAndView("/jsp/CurrencyInfo.jsp");
	String msg=null;
	try{
		cDAO.insert(currency);
		msg="Successfully Saved Currency Details";
	}catch(Exception e){
		e.printStackTrace();
		msg="Failed To Save Currency Details.Please Try Again";
	}
	model.addObject("currencysave",msg);
	model.addObject("submenu", "admin");
	model.addObject("menuName","admin");
	
	return model;
	}
}